package studio.raptor.extractor.segment.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Service;
import studio.raptor.databus.command.Event;
import studio.raptor.databus.command.EventBasicData;
import studio.raptor.databus.command.EventMetaData;
import studio.raptor.extractor.common.constant.EventMetaStatus;
import studio.raptor.extractor.common.holder.ExecutorHolder;
import studio.raptor.extractor.segment.config.ServerConfig;
import studio.raptor.extractor.segment.dao.EventDao;
import studio.raptor.extractor.segment.entity.EventMeta;
import studio.raptor.extractor.segment.server.ExtractorServer;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Created by liuyong8 on 2017/7/18.
 */
@Service
public class EventMetaService {

  private static final Logger log = LoggerFactory.getLogger(ExtractorServer.class);

  @Autowired
  private EventDao eventDao;

  public Long addEventMetaData(EventBasicData eventBasicData, EventMetaData eventMetaData) {
    return eventDao.addEventMeta(eventBasicData, eventMetaData);
  }

  public List<EventMeta> findEventMetas(Integer kindId) throws Exception {
    List<EventMeta> eventMetas =
        eventDao.queryEventMetaByKindIdAndStatus(kindId, EventMetaStatus.STATUS_WAIT.value());
    return eventMetas;
  }

  public List<EventMeta> findMetasByKindIdAndStatuses(Integer kindId,List<String> statuses){
    return eventDao.queryMetasByStatuses(kindId,statuses);
  }

  public void modifyEventMetaStatus2W(EventMeta eventMeta) throws SQLException{
    eventMeta.setStatus(EventMetaStatus.STATUS_WAIT.value());
    eventDao.updateEventMetaStatusByKindIdAndObjectKey(eventMeta);
  }

  public void modifyEventMetaStatus2P(EventMeta eventMeta) throws SQLException {
    eventMeta.setStatus(EventMetaStatus.STATUS_PROCESS.value());
    eventDao.updateEventMetaStatusByKindIdAndObjectKey(eventMeta);
  }

  public void modifyEventMetaStatus2C(EventMeta eventMeta) throws SQLException{
    eventMeta.setStatus(EventMetaStatus.STATUS_COMPLETE.value());
    eventMeta.setRemark("success");
    eventDao.updateEventMetaStatusByKindIdAndObjectKey(eventMeta);
  }

  public void modifyEventMetaStatus2F(EventMeta eventMeta) throws SQLException{
    eventMeta.setStatus(EventMetaStatus.STATUS_FALSE.value());
    eventDao.updateEventMetaStatusByKindIdAndObjectKey(eventMeta);
  }

}
